goog.require('dkpoints.nav.AdminRaidHandler');
goog.require('dkpoints.tests.mocks');
goog.require('dkpoints.tests.stubFn');


var stubFn, handler, bodyContent, rpc, status,
    realRaid, realRaidDriver, mockRaid, mocks;


function setUpPage() {
  mocks = dkpoints.tests.mocks;
  stubFn = dkpoints.tests.stubFn;
}

function setUp() {
  bodyContent = {};
  rpc = {
    execute: stubFn()
  };
  status = {};
  sp = {
    getBodyContent: stubFn(bodyContent),
    getRPC: stubFn(rpc),
    getStatusManager: stubFn(status)
  };

  realRaidDriver = dkpoints.raid.RaidDriver;

  dkpoints.raid.RaidDriver = stubFn();
  dkpoints.raid.RaidDriver.prototype.createNewRaid = stubFn();

  handler = new dkpoints.nav.AdminRaidHandler(sp);
}


/**
 * Test that the constructor properly stores references to its dependencies.
 */
var testConstructorStoresDependencies = function() {
  assertTrue('sp.getRPC should have been called.', sp.getRPC.called);
  assertEquals('Handler should have stored a reference to the rpc service.',
               rpc, handler.rpc_);

  assertTrue('sp.getStatusManager should have been called.',
             sp.getStatusManager.called);
  assertEquals('Handler should have stored a reference to the status manager.',
               status, handler.status_);

  assertTrue('sp.getBodyContent should have been called.',
             sp.getBodyContent.called);
  assertEquals('Handler should have stored a reference to the bodyContent el.',
               bodyContent, handler.bodyContent_);

  assertNull('Handler.raidDriver should default to null.', handler.raidDriver_);
};


/**
 * Test that handler.handle creates a new Raid Driver object.
 */
var testHandleCreatesNewRaidDriverIfMissing = function() {
  var rdCtor = dkpoints.raid.RaidDriver;

  assertNull('Handler.raidDriver_ should default to null on construction.',
             handler.raidDriver_);
  handler.handle(mocks.getMockPath('/admin/raid', '/admin/raid', ''));

  assertTrue('Handler.raidDriver_ should be an instance of RaidDriver',
             handler.raidDriver_ instanceof rdCtor);

  assertEquals('RaidDriver ctor should have been given the rpc service.',
               rpc, rdCtor.args[0]);
  assertEquals('RaidDriver ctor should have been given the status manager.',
               status, rdCtor.args[1]);
  assertEquals('RaidDriver ctor should have been given bodyContent element.',
               bodyContent, rdCtor.args[2]);
};


/**
 * Test that handler.handle re-uses a pre-existing raid driver if one already
 * exists.
 */
var testHandleReusesRaidDriverIfPresent = function() {
  var driver = handler.raidDriver_ = new dkpoints.raid.RaidDriver(),
      rdCtor = dkpoints.raid.RaidDriver;

  // Reset from creating the mock.
  rdCtor.called = false;

  handler.handle(mocks.getMockPath('/admin/raid', '/admin/raid', ''));
  assertFalse('Raid Driver ctor should not have been called.', rdCtor.called);
};


/**
 * Test that handler.handle calls driver.createNewRaid if path.params is empty.
 */
var testHandleCallsDriverCreateNewRaidWithEmptyPathParams = function() {
  var driver = handler.raidDriver_ = {
                 createNewRaid: stubFn()
               },
      mockPath = mocks.getMockPath('/admin/raid', '/admin/raid', '');


  handler.handle(mockPath);
  assertTrue('Handle should have called raidDriver.createNewRaid.',
             driver.createNewRaid.called);
  assertEquals('RaidDriver.createNewRaid should have been given our path.',
               mockPath, driver.createNewRaid.args[0]);
};


function tearDown() {
  handler = null;
  dkpoints.raid.RaidDriver = realRaidDriver;
}
